package models

import (
	"base_srv/basic/config"
	"time"
)

type CarAppUpdate struct {
	Id              uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	CarAppName      string    `gorm:"column:car_app_name;type:varchar(30);not null;" json:"car_app_name"`
	CarAppVersion   string    `gorm:"column:car_app_version;type:varchar(20);not null;" json:"car_app_version"`
	VersionCode     string    `gorm:"column:version_code;type:varchar(20);not null;" json:"version_code"`
	UpdateText      string    `gorm:"column:update_text;type:varchar(50);not null;" json:"update_text"`
	UserId          int32     `gorm:"column:user_id;type:int;not null;" json:"user_id"`
	UploadTime      time.Time `gorm:"column:upload_time;type:datetime;not null;" json:"upload_time"`
	StartUpdateTime time.Time `gorm:"column:start_update_time;type:datetime;not null;" json:"start_update_time"`
	CreateTime      time.Time `gorm:"column:create_time;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_time"`
	UpdateTime      time.Time `gorm:"column:update_time;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_time"`
	DeleteTime      time.Time `gorm:"column:delete_time;type:datetime;default:NULL;" json:"delete_time"`
}

func (c *CarAppUpdate) TableName() string {
	return "car_app_update"
}

func (c *CarAppUpdate) Create() error {
	return config.DB.Create(&c).Error
}
func (c *CarAppUpdate) Updates() error {
	return config.DB.Updates(&c).Error
}
func (c *CarAppUpdate) Delete() error {
	return config.DB.Delete(&c).Error
}

func (c *CarAppUpdate) GetList() (list []CarAppUpdateResp, err error) {
	err = config.DB.Model(&CarAppUpdate{}).Select("car_app_update.id,car_app_update.car_app_name,car_app_update.car_app_version,car_app_update.version_code," +
		"car_app_update.update_text,car_app_update.upload_time,car_app_update.start_update_time,user.real_name").
		Joins("left join user on user.id = car_app_update.user_id").
		Where("car_app_update.delete_time is null").Order("car_app_update.id desc").Find(&list).Error
	return
}

type CarAppUpdateResp struct {
	Id              uint32    `json:"id"`
	CarAppName      string    `json:"carAppName"`
	CarAppVersion   string    `json:"carAppVersion"`
	VersionCode     string    `json:"versionCode"`
	UpdateText      string    `json:"updateText"`
	RealName        string    `json:"realName"`
	UploadTime      time.Time `json:"uploadTime"`
	StartUpdateTime time.Time `json:"startUpdateTime"`
}
